﻿using RazorEngine;
using RazorEngine.Templating;
using Fortunate.Application;
using SqlSugar;

namespace classGenerate
{
    public class Generate
    {
        //存储路径
        private static string directoryPath = "E:\\GIT存放\\fortunate\\fortunate\\Fortunate.Application\\Entities";

        /// <summary>
        /// 数据库生成表方法一
        /// </summary>
        public static void GenerateDbFirst1()
        {
            ConnectionConfig connectionConfig = new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.SqlServer,//数据库类型
                ConnectionString = "server=localhost;uid=sa;pwd=123;database=testDB;",//配置文件中的数据库链接key值
                IsAutoCloseConnection = true,//是否自动关闭连接
            };
            using (var db = new SqlSugarClient(connectionConfig))
            {

                db.DbFirst.IsCreateAttribute().CreateClassFile(directoryPath, Helper.GetnameSpace);
                //string 带？
                db.DbFirst.StringNullable().CreateClassFile(directoryPath, Helper.GetnameSpace);
                //生成带SqlSugar特性
                db.DbFirst.IsCreateAttribute().CreateClassFile(directoryPath, Helper.GetnameSpace);
            }
        }

        /// <summary>
        /// 数据库生成表方法二
        /// </summary>
        public static void GenerateDbFirst2()
        {
            //生成class文件
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server=localhost;uid=sa;pwd=123;database=testDB;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    RazorService = new RazorService()
                }
            });
            //模板
            string DefaultRazorClassTemplate = RazorFirst.DefaultRazorClassTemplate;
            string DefaultRazorClassTemplateNew = "using System;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing SqlSugar;\r\nnamespace @Model.Namespace \r\n{\r\n    ///<summary>\r\n    ///\r\n    ///</summary>\r\n    public  class @Model.ClassName\r\n    {\r\n  @foreach (var item in @Model.Columns)\r\n   {\r\n      if(item.IsPrimarykey&&item.IsIdentity){\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>     \r\n         @:[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]      \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n        else if(item.IsPrimarykey)\r\n        {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>    \r\n         @:[SqlSugar.SugarColumn(IsPrimaryKey = true)]       \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         } \r\n        else if(item.IsIdentity)\r\n        {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>       \r\n         @:[SqlSugar.SugarColumn(IsIdentity = true)]    \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n         else\r\n         {\r\n         @:/// <summary>\r\n         @:/// Desc:@item.ColumnDescription\r\n         @:/// Default:@item.DefaultValue\r\n         @:/// Nullable:@item.IsNullable\r\n         @:/// </summary>           \r\n         @:public @item.DataType @item.DbColumnName {get;set;}\r\n         }\r\n       }\r\n\r\n    }\r\n}";
            db.DbFirst.UseRazorAnalysis(DefaultRazorClassTemplateNew).CreateClassFile(directoryPath);

        }
    }
    public class RazorService : IRazorService
    {
        public List<KeyValuePair<string, string>> GetClassStringList(string razorTemplate, List<RazorTableInfo> model)
        {
            if (model != null && model.Any())
            {
                var result = new List<KeyValuePair<string, string>>();
                foreach (var item in model)
                {
                    try
                    {
                        item.ClassName = item.DbTableName;//格式化类名
                        string key = "key" + razorTemplate.Length;
                        var classString = Engine.Razor.RunCompile(razorTemplate, key, item.GetType(), item);
                        result.Add(new KeyValuePair<string, string>(item.ClassName, classString));
                    }
                    catch (Exception ex)
                    {
                        new Exception(item.DbTableName + " error ." + ex.Message);
                    }
                }
                return result;
            }
            else
            {
                return new List<KeyValuePair<string, string>>();
            }
        }
    }


}
